import request from './request'

// const baseHost = 'http://192.168.31.245:9097/';
const baseHost = 'https://jjyp-api.nervenets.cn/';
const baseURL = baseHost + 'api/';
request.config.baseURL = baseURL;

function fail(res) {
	let message = '网络错误或服务器异常，请稍后再试！'
	if (401 != res.status) {
		if (res.response && res.response.data) {
			message = res.response.data.message
		}

		uni.showModal({
			title: '提示',
			content: message,
			success: function(res) {
				if (res.confirm) {
					console.log('用户点击确定');
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	} else {
		uni.removeStorageSync('token');
		uni.removeStorageSync('uid');
	}
	return res;
}

function success(res) {
	if (res.code == 401) {
		uni.removeStorageSync('token');
		uni.removeStorageSync('uid');
	} else if (res.code == 200) {

	}
	return res;
}

var get_headers = () => {
	let getJosn = {
		headers: {
			"content-type": "application/json",
			"User-Platform": "miniApp",
			"User-Token": uni.getStorageSync('token')
		}
	}
	return getJosn
}

const api = {
	baseHost: baseHost,
	baseURL: baseURL,
	base64Upload: baseURL + 'file/base64Upload',

	//微信授权登录
	waLogin: (data, headers = get_headers()) => request.post('user/waLogin', data, headers).then(success).catch(
		fail),
	//用户列表
	userList: (data, headers = get_headers()) => request.post('user/list', data, headers).then(success).catch(fail),
	// /api/week/data/list// 周统计列表

	weekDataList: (data, headers = get_headers()) => request.post('week/data/list', data, headers).then(success)
		.catch(fail),
	//用户详情
	userDetail: (data, headers = get_headers()) => request.post('user/detail', data, headers).then(success).catch(
		fail),
	///api/user/loginInfo登录信息
	loginInfo: (data, headers = get_headers()) => request.post('user/loginInfo', data, headers).then(success).catch(
		fail),
	//查询当前用户信息
	my: (data, headers = get_headers()) => request.post('user/my', data, headers).then(success).catch(fail),
	//更新用户信息
	updateUserInfo: (data, headers = get_headers()) => request.post('user/updateUserInfo', data, headers).then(
		success).catch(
		fail),
	//验证码发送
	validationCode: (data, headers = get_headers()) => request.post('user/validationCode', data, headers).then(
		success).catch(
		fail),
	//绑定手机号
	bindMobile: (data, headers = get_headers()) => request.post('user/bindMobile', data, headers).then(success)
		.catch(
			fail),
	////api/groupbuy/order/list 列表  拼团管理相关接口
	groupbuyList: (data, headers = get_headers()) => request.post('groupbuy/order/list', data, headers)
		.then(success).catch(
			fail),
	///api/groupbuy/order/user/list列表// 拼团订单管理相关接口
	groupbuyOrderList: (data, headers = get_headers()) => request.post('groupbuy/order/user/list', data, headers)
		.then(success).catch(
			fail),
	//商品列表 /api/product/list
	productList: (data, headers = get_headers()) => request.post('product/list', data, headers)
		.then(success).catch(
			fail),
	//商品详情 /api/product/detail
	productDetail: (data, headers = get_headers()) => request.post('product/detail', data, headers)
		.then(success).catch(
			fail),
	// /api/groupbuy/order/user/pay  支付
	groupbuyOrderPay: (data, headers = get_headers()) => request.post('groupbuy/order/user/pay', data, headers)
		.then(success)
		.catch(fail),
	///api/groupbuy/order/user/edit
	groupbuyOrderEdit: (data, headers = get_headers()) => request.post('groupbuy/order/user/edit', data, headers)
		.then(success).catch(
			fail),
	////api/groupbuy/order/user/list 列表
	groupbuyOrderList: (data, headers = get_headers()) => request.post('groupbuy/order/user/list', data, headers)
		.then(success).catch(
			fail),
	///api/groupbuy/order/user/detai
	groupbuyOrderDetail: (data, headers = get_headers()) => request.post('groupbuy/order/user/detail', data,
			headers)
		.then(success).catch(
			fail),
	///api/groupbuy/order/user/finish
	groupbuyOrderFinish: (data, headers = get_headers()) => request.post('groupbuy/order/user/finish', data,
			headers)
		.then(success).catch(
			fail),
	///constant all
	constantAll: (data, headers = get_headers()) => request.post('constant/all', data, headers).then(success).catch(
		fail),
	// /api/groupbuy/order/user/grouped/notify
	// 查询拼团成功的通知
	groupbuyOrderNotify: (data, headers = get_headers()) => request.post('groupbuy/order/user/grouped/notify', data,
			headers)
		.then(success).catch(
			fail),
	// /api/groupbuy/order/user/notify/confirm
	// 确认通知
	groupbuyOrderConfirm: (data, headers = get_headers()) => request.post('groupbuy/order/user/notify/confirm',
			data, headers)
		.then(success).catch(
			fail),
	appUserState: (data, headers = get_headers()) => request.post('app/user/state',
			data, headers)
		.then(success).catch(
			fail),

}
export default api
